orderController = BaseController.extend({
	template:'orders',
	center:'订单列表',
	mainView: 'order',
	bottomShow: true,
	waitOn:function(){
		let self = this;
		self.limit = 30;
		self.order_selector = {};
		Tracker.autorun(function(){
			self.merchantInfo = self.currentUser();
			if (self.merchantInfo) {
				self.order_selector = {state:'pay', merchant_id: parseInt(self.merchantInfo.id)};
				
				Meteor.subscribe('goods', {merchant_id: parseInt(self.merchantInfo.id)}, {name:1,id:1});
				
				return self.subscribeHandleDict['orders'] = Meteor.subscribe('orders', self.order_selector, { sort: { id: -1 }, limit: self.limit }, self.subscribeCallbacks());
			}
		})
	},
	onCreated:function(){

	},
	onRendered:function(){
		let self = this;

		self.paging({ selector: self.order_selector, sorts: self.sorts, perpages: 4 }, function (options) {
			self.scrolling = true;
			self.limit += options.perpages;
			let subscribe = self.subscribeHandleDict['refunds'];

			if (subscribe) {
				subscribe.stop();
			}
		});

		$('#startTime').val();
		$('#endTime').val();
	},
	getOrder: function () {
		this.scrolling = false;
		return this.subscribeHandleDict['orders'] = Meteor.subscribe('orders', this.order_selector, { sort: { id: -1 }, limit: this.limit }, this.subscribeCallbacks());
	},
	data: function() {
		let self = this;
		return {
			scroll: 'infinite-scroll',
			goodsList: function(){
				return Goods.find().fetch();
			},
			ordersList: function(){
				return Orders.find({}, {sort: { id: -1}}).fetch();
			}
		};
	},
	getTime: function () {
		let self = this;
		let start_time = $('#startTime').val();
		let end_time = $('#endTime').val();

		if(start_time && end_time){
			let start = self.dateToTime(start_time + " 00:00:00");
			let end = self.dateToTime(end_time + " 23:59:59");
			if (start > end){
				self.app.alert('开始日期不能大于结束日期，请重新选择', function(){
					$('#startTime').val();
					$('#endTime').val();
				});
			}else {
				self.order_selector.pay_time_int = {"$lte": end, "$gte": start};
				
				self.lastIndex = self.limit;
				self.subscribeHandle = self.getOrder;
				let subscribe = self.subscribeHandleDict['orders'];
				if (subscribe) {
					subscribe.stop();
				}
			}
		}
	},
	events: function (controller) {
		let self = this;
		return {
			'change #sid': function (e) {
				var selectValue = $('#sid').val();
					console.log(selectValue)
				if(selectValue){
					self.order_selector.goods_id = parseInt(selectValue);
						
					self.lastIndex = self.limit;
					self.subscribeHandle = self.getOrder;
					let subscribe = self.subscribeHandleDict['orders'];
					if (subscribe) {
						subscribe.stop();
					}
				}else{
					self.order_selector = {state:'pay', merchant_id: parseInt(self.merchantInfo.id)};

					self.lastIndex = self.limit;
					self.subscribeHandle = self.getOrder;
					let subscribe = self.subscribeHandleDict['orders'];
					if (subscribe) {
						subscribe.stop();
					}
				}
			},
			'change #startTime': function (e) {
				self.getTime();
			},
			'change #endTime': function (e) {
				self.getTime();
			}
		}
	}
})